﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XPSReaderWPF
{
    /// <summary>
    /// Interaction logic for Reader.xaml
    /// </summary>
    public partial class Reader : Window
    {
        string _filename;
        #region Filename
        /// <summary>
        /// Gets or sets the filename.
        /// </summary>
        /// <value>The filename.</value>
        public string Filename
        {
            get { return _filename; }
            set { _filename = value; }
        }
        #endregion

        int _nbPage;
        #region NbPage
        /// <summary>
        /// Gets or sets the nb page.
        /// </summary>
        /// <value>The nb page.</value>
        public int NbPage
        {
            get { return _nbPage; }
            set { _nbPage = value; }
        }
        #endregion

        double _pageHeight;
        #region PageHeight
        /// <summary>
        /// Gets or sets the height of the page.
        /// </summary>
        /// <value>The height of the page.</value>
        public double PageHeight
        {
            get { return _pageHeight; }
            set { _pageHeight = value; }
        }
        #endregion

        double _pageWidth;
        #region PageWidth
        /// <summary>
        /// Gets or sets the width of the page.
        /// </summary>
        /// <value>The width of the page.</value>
        public double PageWidth
        {
            get { return _pageWidth; }
            set { _pageWidth = value; }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Reader"/> class.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="nbPage">The nb page.</param>
        public Reader(string filename, int nbPage, double pageHeight, double pageWidth)
        {
            InitializeComponent();
            Filename = filename;
            NbPage = nbPage;
            PageHeight = pageHeight;
            PageWidth = pageWidth;
        }

        /// <summary>
        /// Handles the SizeChanged event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param>
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // to resize the control Book when the Window change size
            ScaleTransform st = new ScaleTransform();

            // When the height page is heigher than the width page's
            if (PageHeight >= PageWidth)
            {
                GridLength lColonne = new GridLength(GridBook.ColumnDefinitions[1].ActualWidth);

                double longCol = lColonne.Value - livreOuvert.ActualWidth + 65;
                longCol /= 2;

                Thickness newMargin = new Thickness(longCol, 15, longCol, 15);
                dockPanel1.Margin = newMargin;
            }
            // When the WindowState isn't maximized and the width page is heigher than the height page's
            else if (this.WindowState != WindowState.Maximized)
            {
                GridLength lColonne = new GridLength(GridBook.ColumnDefinitions[1].ActualWidth);

                double longCol = lColonne.Value - livreOuvert.ActualHeight + 10;
                longCol /= 2;

                Thickness newMargin = new Thickness(longCol, 33, longCol, 33);
                dockPanel1.Margin = newMargin;
            }
            // When the WindowState is maximized and the width page is heigher than the height page's
            else if (this.WindowState == WindowState.Maximized)
            {
                Thickness newMargin = new Thickness(275, 33, 275, 33);
                dockPanel1.Margin = newMargin;
            }

        }

        /// <summary>
        /// Handles the MouseDoubleClick event of the myBook control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void myBook_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Point pMouse;

            pMouse = e.GetPosition(myBook);

            if ((PageHeight > PageWidth && pMouse.X > (myBook.ActualWidth / 2)) || (PageHeight < PageWidth && pMouse.X > (myBook.ActualWidth / 2)))
                myBook.AnimateToNextPage(true, 500);
            else
                myBook.AnimateToPreviousPage(true, 500);

        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            object itemPage;
            UCPng bt;
            RotateTransform rt;

            // Loading all png files in the Book control
            for (int i = 0; i < NbPage; i++)
            {
                string source = Filename + ".Page" + i + ".png";
                if (PageHeight < PageWidth)
                    bt = new UCPng(source, true);
                else
                    bt = new UCPng(source, false);

                itemPage = (object)bt;
                myBook.Items.Add(itemPage);
            }

            this.Title = Filename;

            // When the width page is heigher than the height page's
            if (PageHeight < PageWidth)
            {
                rt = new RotateTransform(90);
                livreOuvert.LayoutTransform = rt;
                dockPanel1.LayoutTransform = rt;

                Thickness newMargin = new Thickness(275, 33, 275, 33);
                dockPanel1.Margin = newMargin;

                WindowBook.MinWidth = 650;
                WindowBook.MinHeight = 800;
            }

            // The case where a document have a single page
            if (NbPage == 1)
            {
                buttonNext.Visibility = Visibility.Hidden;
                buttonPrevious.Visibility = Visibility.Hidden;
            }

        }

        /// <summary>
        /// Handles the Closed event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        /// <summary>
        /// Handles the Click event of the buttonPrevious control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonPrevious_Click(object sender, RoutedEventArgs e)
        {
            if (myBook.CurrentSheetIndex > 0)
            {
                // Animation to turn the previous page
                myBook.AnimateToPreviousPage(true, 500);
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            if (myBook.CurrentSheetIndex < NbPage / 2)
            {
                // Animation to turn the next page
                myBook.AnimateToNextPage(true, 500);
            }
        }



    }
}
